package com.boxuegu.boxuegu.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.boxuegu.boxuegu.R;
import com.boxuegu.boxuegu.bean.ExercisesDetailBean;

import java.io.PipedOutputStream;
import java.util.ArrayList;

public class ExercisesDetailAdapter extends BaseAdapter {
    private ArrayList<ExercisesDetailBean> beans;
    private Context context;

    public ExercisesDetailAdapter(Context context ) { this.context=context; }

    public void setData(ArrayList<ExercisesDetailBean> beans) {//为属性beans赋值
        this.beans = beans;
        notifyDataSetChanged();//原来的数据源的内容可能已经填充到了listview中，用这个方法会更新listview的所有行
    }//为属性beans赋值

    @Override
    public int getCount() {
        if(beans==null)return 0;
        return beans.size();
    }

    @Override
    public Object getItem(int position) {
        if(beans==null)return null;
        return beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //listview每显示一行就执行该方法一次，生成这一行position的行控件view
        //如果屏幕只能显示4行，则该方法先运行5次，当滚动要显示第6行时，则利用第1行的控件
        ViewHolder viewHolder=null;
        if(convertView==null){//条件成立表示当前还没有完全滚走的行
            LayoutInflater layoutInflater=LayoutInflater.from(context);
            convertView=layoutInflater.inflate(R.layout.exercises_list_item,null);
            viewHolder=new ViewHolder();
            viewHolder.tv_subject=convertView.findViewById(R.id.tv_subject);
            viewHolder.tv_a=convertView.findViewById(R.id.tv_a);
            viewHolder.tv_b=convertView.findViewById(R.id.tv_b);
            viewHolder.tv_c=convertView.findViewById(R.id.tv_c);
            viewHolder.tv_d=convertView.findViewById(R.id.tv_d);
            viewHolder.iv[0]=convertView.findViewById(R.id.tv_a);
            viewHolder.iv[1]=convertView.findViewById(R.id.tv_b);
            viewHolder.iv[2]=convertView.findViewById(R.id.tv_c);
            viewHolder.iv[3]=convertView.findViewById(R.id.tv_d);
            convertView.setTag(viewHolder);

        }else {//已经滚走的行，新的行就重复利用滚走的行，要清除作答信息
            if(beans.get(position).selected==0){//当前滚入的position选择题，前面用户没有做过答
                viewHolder=(ViewHolder)convertView.getTag();
                viewHolder.iv[0].setImageResource(R.drawable.exercises_a);
                viewHolder.iv[1].setImageResource(R.drawable.exercises_b);
                viewHolder.iv[2].setImageResource(R.drawable.exercises_c);
                viewHolder.iv[3].setImageResource(R.drawable.exercises_d);
                for(int i=0;i<4;i++) {
                    viewHolder.iv[i].setEnabled(true);
                }
            }else {
                viewHolder=(ViewHolder)convertView.getTag();
            }
        }
        viewHolder.tv_subject.setText(beans.get(position).subject);
        viewHolder.tv_a.setText(beans.get(position).a);
        viewHolder.tv_b.setText(beans.get(position).b);
        viewHolder.tv_c.setText(beans.get(position).c);
        viewHolder.tv_d.setText(beans.get(position).d);

        final  ViewHolder vh=viewHolder;

        viewHolder.iv[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRightError(position,vh,1);
            }
        });
        viewHolder.iv[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRightError(position,vh,2);
            }
        });
        viewHolder.iv[2].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRightError(position,vh,3);
            }
        });
        viewHolder.iv[3].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRightError(position,vh,4);
            }
        });
        return convertView;
    }
    class ViewHolder{
        public TextView tv_subject;
        public  TextView tv_a,tv_b,tv_c,tv_d;
        public ImageView[] iv=new ImageView[4];
    }

    private  void setRightError(int position,ViewHolder vh,int selected){
        beans.get(position).selected=selected;
        if (beans.get(position).answer==selected){
            vh.iv[selected -1].setImageResource(R.drawable.exercises_right_icon);
        }else {
            vh.iv[selected -1].setImageResource(R.drawable.exercises_error_icon);
            vh.iv[beans.get(position).answer -1].setImageResource(R.drawable.exercises_right_icon);
        }

        for (int i=0;i<4;i++){
            vh.iv[i].setEnabled(false);
        }
    }
}
